﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Loli.Library.WinForm
{
    public class TipListBox : ListBox
    {
        ToolTip tip;
        private int _OldIndex = -1;

        public TipListBox()
        {
            this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
           // tip = new ToolTip();
        }

        private void SetTipMessage(string strTip)
        {
            tip.SetToolTip(this, strTip);
        }
        /// <summary>  
        /// 重写鼠标移动事件  
        /// </summary>  
        /// <param name="e"></param>  
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            /*
            int idx = IndexFromPoint(e.Location);// 获取鼠标所在的项索引  
            if (idx == -1) //鼠标所在位置没有 项  
            {
                tip.Hide(this);
                // SetTipMessage(""); //设置提示信息为空  
                return;
            }
            if (_OldIndex != idx && idx < this.Items.Count)
            {
                string txt = GetItemText(this.Items[idx]); //获取项文本  
                SetTipMessage(txt); //设置提示信息  
                _OldIndex = idx;
            }*/
        }

        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            base.OnMeasureItem(e);
            e.ItemHeight = this.ItemHeight;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            e.DrawBackground();
            e.DrawFocusRectangle();


            if (this.Items.Count > 0)
            {

                object obj = this.Items[e.Index];

                if (obj != null && !string.IsNullOrEmpty(this.DisplayMember))
                {

                    string display = obj.GetType().GetProperty(this.DisplayMember).GetValue(obj).ToString();

                    StringFormat drawFormat = new StringFormat();
                    //drawFormat.Alignment = StringAlignment.Center;
                    drawFormat.Trimming = StringTrimming.Character;
                    drawFormat.FormatFlags = StringFormatFlags.NoWrap;
                    // drawFormat.LineAlignment = StringAlignment.

                    e.Graphics.DrawString(display, e.Font, new SolidBrush(e.ForeColor), e.Bounds, drawFormat);
                }
            }
        }
    }
}
